package com.netty.http.handler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.net.URI;
import java.nio.charset.Charset;

import com.netty.collection.EntityContainer;
import com.netty.handler.BootHandler;
import com.netty.http.HttpResponseMsg;
import com.netty.resolver.ResourceResolver;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;

public class HtmlHttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {
	String charset = "UTF-8";

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
		if (msg instanceof HttpRequest) {
			HttpRequest request = (HttpRequest) msg;
			URI uri = new URI(request.uri());
			System.out.println("request uri==" + uri.getPath());
			if (uri.getPath().equals("/")) {
				String name = "/index.html";
				System.out.println("html 文件名： " + name);
				InputStream is = ResourceResolver.getResource(name);

				HttpResponseMsg hrm = new HttpResponseMsg();
				hrm.setResType(HttpResponseMsg.ResType.HTML.getValue());
				hrm.setResCode(HttpResponseMsg.ResCode.OK.getValue());
				hrm.setMessage(convertStreamToString(is));
				ctx.writeAndFlush(hrm);
			}
			if (uri.getPath().equals("/favicon.ico")) {
				HttpResponseMsg hrm = new HttpResponseMsg();
				hrm.setResType(HttpResponseMsg.ResType.HTML.getValue());
				hrm.setResCode(HttpResponseMsg.ResCode.NOT_FOUND.getValue());
				hrm.setMessage("你来到了无知的海洋！");
				ctx.writeAndFlush(hrm);
				return;
			}
			if (uri.getPath().matches("/html/[a-zA-Z]+.html")) {
				String path = uri.getPath();
				String name = path.substring(path.lastIndexOf("/"), path.length());
				System.out.println("html 文件名： " + name);
				InputStream is = ResourceResolver.getResource(name);

				HttpResponseMsg hrm = new HttpResponseMsg();
				hrm.setResType(HttpResponseMsg.ResType.HTML.getValue());
				hrm.setResCode(HttpResponseMsg.ResCode.OK.getValue());
				hrm.setMessage(convertStreamToString(is));
				ctx.writeAndFlush(hrm);
			} else if (uri.getPath().matches("/js/.*.js")) {
				String path = uri.getPath();
				String name = path.substring(path.lastIndexOf("/"), path.length());
				System.out.println("js 文件名： " + name);
				InputStream is = ResourceResolver.getResourceJs(name);
				HttpResponseMsg hrm = new HttpResponseMsg();
				hrm.setResType(HttpResponseMsg.ResType.JS.getValue());
				hrm.setResCode(HttpResponseMsg.ResCode.OK.getValue());
				hrm.setMessage(convertStreamToString(is));
				ctx.writeAndFlush(hrm);
			} else {
				String path = uri.getPath();
				if (path.matches("/[A-Za-z-_]+/[A-Za-z-_]+")) {
					ctx.writeAndFlush(BootHandler.doTrans(request));
					return;
				}
				
				HttpResponseMsg hrm = new HttpResponseMsg();
				hrm.setResType(HttpResponseMsg.ResType.HTML.getValue());
				hrm.setResCode(HttpResponseMsg.ResCode.NOT_FOUND.getValue());
				hrm.setMessage("你来到了无知的海洋！");
				ctx.writeAndFlush(hrm);
			}
		}
	}

	public String convertStreamToString(InputStream is) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();

	}
}
